﻿using System;
using System.Windows.Forms;
using KZ.Lottery.BLL;
using KZ.Lottery.Resources;

namespace KZ.Lottery
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            if (!SettingBLL.CheckConnection())
            {
                FrmConnection frm = new FrmConnection();
                frm.ShowDialog();
            }

            if (!SettingBLL.CheckSetting())
            {
                FrmSetting frm = new FrmSetting();
                frm.ShowDialog();
            }

            FrmLogin frmLogin = new FrmLogin();
            if (frmLogin.ShowDialog() == DialogResult.Yes)
            {
                Global.OpenTab(new FrmDashboard());
            }
            else
            {
                Application.Exit();
            }
        }

        private void trvMenu_DoubleClick(object sender, EventArgs e)
        {
            TreeNode node = trvMenu.SelectedNode;
            switch (node.Name)
            {
                case "nodeDashboard":
                    Global.OpenTab(new FrmDashboard());
                    break;
                case "nodeRule":
                case "nodeRuleList":
                    Global.OpenTab(new FrmRule());
                    break;
                case "nodeRuleInsert":
                    FrmRuleInsert frmRuleInsert = new FrmRuleInsert();
                    frmRuleInsert.Action = KZ_Resource.Action_Insert;
                    frmRuleInsert.ShowDialog();
                    break;
                case "nodeSchedule":
                    FrmSchedule frmSchedule = new FrmSchedule();
                    frmSchedule.ShowDialog();
                    break;
                case "nodeResult":
                case "nodeResultList":
                    Global.OpenTab(new FrmResult());
                    break;
                case "nodeResultInsert":
                    FrmResultInsert frmResultInsert = new FrmResultInsert();
                    frmResultInsert.Action = KZ_Resource.Action_Insert;
                    frmResultInsert.ShowDialog();
                    break;
                case "nodeReportByRule":
                    FrmReport frmReportByRule = new FrmReport();
                    frmReportByRule.ReportType = "ReportByRule";
                    frmReportByRule.ShowDialog();
                    break;
                case "nodeReportByChannel":
                    FrmReport frmReportByDate = new FrmReport();
                    frmReportByDate.ReportType = "ReportByChannel";
                    frmReportByDate.ShowDialog();
                    break;
                case "nodeSetting":
                    FrmSetting frmSetting = new FrmSetting();
                    frmSetting.ShowDialog();
                    break;
                case "nodeChangePassword":
                    FrmChangePassword frmChangePassword = new FrmChangePassword();
                    frmChangePassword.ShowDialog();
                    break;
            }
        }

        private void tsbClose_Click(object sender, EventArgs e)
        {
            tbcMain.TabPages.RemoveByKey(tbcMain.SelectedTab.Name);
            TabChanged();
        }

        public void TabChanged()
        {
            tsbClose.Visible = tbcMain.TabCount > 0;
        }
    }
}